﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace psafth.RCTiming.Main.Pages.Dialogs
{
    /// <summary>
    /// Interaction logic for AddClubDialog.xaml
    /// </summary>
    public partial class AddClubDialog : Window
    {
        AddDriverDialog _driverDialog;

        private MainWindow _mainWindow;

        public DBContext DbContext
        {
            get
            {
                return _driverDialog.DbContext;
            }
            set
            {
                _driverDialog.DbContext = value;
            }
        }

        public AddClubDialog()
        {
            InitializeComponent();
        }

        public AddClubDialog(AddDriverDialog driverDialog)
        {
            InitializeComponent();

            _driverDialog = driverDialog;

            tbFullName.Text = _driverDialog.cbClub.Text;

            cbCountry.ItemsSource = GetCountryList().OrderBy(c => c);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(tbFullName.Text) && !string.IsNullOrWhiteSpace(tbLicencePrefix.Text) && !string.IsNullOrWhiteSpace(tbShortName.Text) && !string.IsNullOrWhiteSpace(cbCountry.Text))
            {
                try
                {
                    Club club = DbContext.Clubs.Add(new Club() { FullName = tbFullName.Text, ShortName = tbShortName.Text, LicencePrefix = tbLicencePrefix.Text, Country = cbCountry.Text });
                    DbContext.SaveChanges();

                    if (_driverDialog != null)
                    {
                        _driverDialog.FetchAndBindClubs();
                        _driverDialog.cbClub.SelectedItem = (from Club c in _driverDialog.cbClub.Items
                                                             where c.Id.Equals(club.Id)
                                                             select c).FirstOrDefault();

                        _driverDialog.cbClub_SelectionChanged(null, null);
                    }

                    this.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ett fel inträffade när klubb skulle sparas. " + ex.Message, "Fel!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show("Alla fält måste fyllas i!", "Fel!", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public static List<string> GetCountryList()
        {
            List<string> cultureList = new List<string>();

            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

            foreach (CultureInfo culture in cultures)
            {
                RegionInfo region = new RegionInfo(culture.LCID);

                if (!(cultureList.Contains(region.EnglishName)))
                {
                    cultureList.Add(region.EnglishName);
                }
            }
            return cultureList;
        }
    }
}
